﻿module RPG {
    export class Level1 extends Phaser.State {
        levelMap: Phaser.Tilemap;

        obstacles: Phaser.TilemapLayer;
        structures: Phaser.TilemapLayer;
        road: Phaser.TilemapLayer;
        trees: Phaser.TilemapLayer;
        treeTops: Phaser.TilemapLayer;

        keys: Phaser.CursorKeys;
        player: RPG.Player;
        collisionLayerGroup: Phaser.TilemapLayer[];

        preload() {
        }

        create() {
            this.levelMap = this.game.add.tilemap('level1_map');
            this.levelMap.addTilesetImage('Environment', 'town_tiles');

            this.levelMap.createLayer('Grass').resizeWorld();

            this.levelMap.createLayer('Road');
            this.structures = this.levelMap.createLayer('Structures');
            this.levelMap.createLayer('Trees');
            this.obstacles = this.levelMap.createLayer('Obstacles');
            this.treeTops = this.levelMap.createLayer('TreeTops');

            this.collisionLayerGroup = [this.obstacles, this.structures, this.road, this.trees, this.treeTops];


            this.levelMap.setCollisionByExclusion([201,202,203,204,205,206,207,208,209,275,276,277,278,279,280,281,282,283,284], true, 'Obstacles');
            this.levelMap.setCollisionByExclusion([158,159,160,161,162,163,164,165,166,167,168,169], true, 'Structures');

            this.player = new Player(this.game, 20, 2600, 'Hero');

            RPG.createRandomPortals(this.game, this.levelMap, this.collisionLayerGroup);

            this.game.world.addAt(this.player, 5);

            this.game.camera.follow(this.player);
        }

        update() {
            this.game.physics.arcade.collide(this.player, this.obstacles);
            this.game.physics.arcade.collide(this.player, this.structures);

            
            /* DEBUG
            var markerX = this.obstacles.getTileX(this.game.input.activePointer.worldX) * 16;
            var markerY = this.obstacles.getTileY(this.game.input.activePointer.worldY) * 16;

            if (this.game.input.mousePointer.isDown) {
                var tile = this.levelMap.getTile(this.obstacles.getTileX(markerX), this.obstacles.getTileY(markerY), 'Obstacles');
                console.log("Tile Properties: ");
                console.log(tile);
                console.log(tile.properties.type);
            }*/
        }
    }
}